<?php
/**
 * Created by PhpStorm.
 * User: FDG367
 * Date: 2018/02/08
 * Time: 15:33
 * detail:商城导览品牌扩展表 510
 */
namespace common\models;

use Yii;
use yii\db\ActiveRecord;

class BrandPlus extends ActiveRecord{

	//是否需要分页
	const PAGE_NUM		= 10;			//分页条数
	public $ispage		= 0;			//是否需要分页
	public $nowpage		= 1;			//当前页
	public $ntoArray	= 0;			//查询时候转化为数据
    public $floor_num;
    public $brand_category_id;
    public static function tableName(){
        return '{{%brand_plus}}';
    }



	/**
	 * @param 获取品牌信息
	 * @param 使用方法 （getBrandPlusInfo->nowpage =2 && getBrandPlusInfo->ispage=1） 获取第二页的数据
	 * @param return Brand&BrandPlusInfo
	 */
	public function getBrandPlusInfo(){

		$params	=	[
			'floor_num'			=>$this->floor_num,
			'brand_category_id'	=>$this->brand_category_id,
			'type'				=>$this->type,
		];

		$params	= array_filter($params);

		//mod by FDG367 2018-02-13 商城导览品牌基础表 510 start
		if(0===$this->floor_num){
			$params=array_merge($params,['floor_num'=>$this->floor_num]);
		}
		//mod by FDG367 2018-02-13 商城导览品牌基础表 510 end
		$info	= self::find()
			->where($params)
			->joinWith('brandRelationPlus',true);

		if($this->ispage){
			$info->limit(self::PAGE_NUM)
				->offset(self::PAGE_NUM *($this->nowpage-1));
		}
		//print_r($info->createCommand()->getRawSql());exit;
		if($this->ntoArray){
			$info->asArray();
		}
		return $info->all();
	}

	/**
	 * @param 关联对应的楼层基本信息
	 * @return \yii\db\ActiveQuery
	 */
	public function getBrandRelationPlus()
	{
		return $this->hasOne(Brand::className(), ['id' => 'brand_id'])->andOnCondition([Brand::tableName().'.use_flg'=>Brand::USE_FLG_YES])->orderBy([
			Brand::tableName().'.sort'=>SORT_ASC,
			Brand::tableName().'.id'=>SORT_ASC,
		]);
	}

    /**
     * @param 获取品牌信息
     * @param 使用方法 （getBrandPlusInfo->nowpage =2 && getBrandPlusInfo->ispage=1） 获取第二页的数据
     * @param return Brand&BrandPlusInfo
     */
    public function getBrandPlusInfoByMobile($select_type = 0){
        $intersect = array();
        //$floor_where = empty($this->floor_num) ? array() : ['floor_num'=>$this->floor_num];
        if($this->floor_num == ''){
            $brand_floor = self::find()->where(['type'=>1])->select('brand_id')->asArray()->all();
        }else{
            $floor_where = ['floor_num'=>$this->floor_num];
            $brand_floor = self::find()->where($floor_where)->select('brand_id')->asArray()->all();
        }
        $id_brand = array_filter(array_unique(array_column($brand_floor,'brand_id')));

        //$brand_where = ['brand_category_id'=>$this->brand_category_id];
        if($this->brand_category_id == ''){
            $brand_cate = self::find()->where(['type'=>2])->select('brand_id')->asArray()->all();
        }else{
            $brand_where	= ['brand_category_id'=>$this->brand_category_id];
            $brand_cate = self::find()->where($brand_where)->select('brand_id')->asArray()->all();
        }
        $id_cate = array_filter(array_unique(array_column($brand_cate,'brand_id')));

        $intersect = array_unique(array_merge($id_brand,$id_cate));
        if(empty($id_brand) && empty($id_cate)){
            return array();
        }

        if(!empty($id_brand) && !empty($id_cate)){//取交集
            $intersect = array_intersect($id_brand,$id_cate);
        }

        /*if($this->floor_num == '' && $this->brand_category_id == ''){
            $intersect = array_unique(array_merge($id_brand,$id_cate));
        }*/

        if(empty($intersect)){
            return array();
        }
        $where = ['brand_id'=>$intersect];

        $params	= array_filter($where);

        $info	= self::find()
            ->where($params)
            ->joinWith('brandRelationPlus',true);

        if($this->ispage){
            $info->limit(self::PAGE_NUM)
                ->offset(self::PAGE_NUM *($this->nowpage-1));
        }

        if($this->ntoArray){
            $info->asArray();
        }


        $result = $info->groupBy('brand_id')->all();
        $brand_id_list = array_column($result,'brand_id');

        $data = array();

        //拼接楼层和分类
        $brand_floor_list = self::find()->where(['brand_id'=>$brand_id_list])->select('brand_id,floor,brand_category_name')->orderBy('floor desc')->asArray()->all();
        $list = array();
        foreach($brand_floor_list as $value){
            $list[$value['brand_id']]['floor'][] = $value['floor'];
            $list[$value['brand_id']]['category_name'][] = $value['brand_category_name'];
        }

        foreach($result as &$res){
            $floor = array_filter(array_unique($list[$res['brand_id']]['floor']));
            $category_name = array_filter(array_unique($list[$res['brand_id']]['category_name']));
            $res['floor'] = implode('/',$floor);
            $res['brand_category_name'] = implode('/',$category_name);
        }
        $data['info'] = $result;

        //获取当前楼层和分类
        /*$cate_list_brand = self::find()->where($params)->asArray()->groupBy('brand_id')->select('brand_id')->all();

        $cate_list_brand_id = array_column($cate_list_brand,'brand_id');*/
        if($select_type == 1){
            $data['list'] = self::getCategoryByFloor($id_brand);
        }else{
            $data['list'] = self::getFloorByCategory($id_cate);
        }

        return $data;
    }

    //根据楼层获取对应分类
    public static function getCategoryByFloor($brand_id_list){
        if(empty($brand_id_list)){
            return array();
        }
        $where[] = 'and';
        $where[] = ['bp.type'=>2];
        $where[] = ['bp.brand_id'=>$brand_id_list];
        $where[] = ['bc.use_flg'=>BrandCategory::USE_FLG_YES];
        $result = BrandPlus::find()->alias('bp')->where($where)->leftJoin('j_brand_category as bc','bc.id = bp.brand_category_id')->asArray()
            ->orderBy('bc.sort asc')->select('bc.*')->all();
        return $result;
    }

    //根据分类获取对应楼层
    public static function getFloorByCategory($brand_id_list){
        if(empty($brand_id_list)){
            return array();
        }
        $result = BrandPlus::find()->alias('bp')->where(['bp.brand_id'=>$brand_id_list,'fc.category_grade'=>1])->leftJoin('j_floor_category as fc','fc.floor_num = bp.floor_num')->asArray()
            ->orderBy('fc.floor_num desc')->select('fc.*')->groupBy('fc.floor')->all();
        return $result;
    }
}